<?php
if ( ! defined('ROOT')) exit('No direct script access allowed');

/**
 * 管理员模型类
 * @author gzute
 * @link http://www.gzute.com
 */

class AdminModel extends Model
{
	public function __construct()
	{
		self::$table = 'admin';
		parent::__construct();
	}
	
	public static function login($condition = array())
	{
		if(count($condition) == 0)
		{
			$condition = array(
				'username' => InterfaceApp::post('username',TRUE),
				'password' => md5(InterfaceApp::post('password',TRUE))
			);
		}
		$return = self::db()->get_where(self::$table,$condition)->row_array();
		if($return){
			$set = array(
                'last_login_time' => $return['now_login_time'],
                'last_login_ip' => $return['now_login_ip'],
                'now_login_ip' => get_ip(),
                'login_times' => $return['login_times'] + 1,
                'now_login_time' => time()
            );
            $where = array('uid' => $return['uid']);
            self::db()->update(self::$table, $set, $where);
            $userdata = array(
                'admin_id' => $return['uid'],
                'admin_name' => $return['username']
            );
            InterfaceApp::set_session($userdata);
            return TRUE;
		}
		return FALSE;
	}
	
	public static function findByid($uid)
	{
		$condition = array('uid'=>$uid);
		return self::db()->get_where(self::$table,$condition)->row_array();
	}
}

//end of this file